
import Common from '../../../public/actions/employing/common';

export default class RowHandle extends Common {
    constructor (comp) {
        super();
        this.comp = comp;
    }

    //指派弹窗确定事件
    getResult = async(value) => {  
        const {props,action} = this.comp
        const {emp,form} = props
        let selectedRows = []
        if(emp.page !== 'main'){
            selectedRows = [{
                ...form.getAllFormValue('card').rows[0]
            }];
        }
        await this.submitOnes(selectedRows,value) 
        action.formAct.toBrowsePage(selectedRows);
    }

    //指派弹窗取消事件
    turnOff = () => {
        const { props } = this.comp
        const { dispatch } = props
        dispatch({
            type: 'emp/update',
            payload: {
                compositedata: null,
                compositedisplay: false,
            }
        })
    }

    // 获取选中的数据
    getSelectedRows = (selectedRows) => {
        const { props } = this.comp;
        const { emp, editTable } = props;

        if (!selectedRows) {
            selectedRows = [];
        }

        if (selectedRows.length <= 0) {
            if (emp.tableSelectAll) {
                selectedRows = editTable.getAllRows('list');
            }
            else {
                selectedRows = editTable.getCheckedRows('list').map((item) => {
                    return item.data;
                });
            }
        }

        if (selectedRows.length <= 0) {
            this.toast({
                color: 'warning',
                content: emp.language['gx6008-000016'] // 请先勾选待操作的数据!
            });
            return false
        }
        return selectedRows
    }

    // 提交某个单据
    submitOnes = async (selectedRows = [],compositepostdata = null) => {
        const { props, action } = this.comp;
        const { emp, dispatch } = props;

        selectedRows = this.getSelectedRows(selectedRows);
        if (selectedRows) {
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk
            };

            postData.billids = selectedRows.map((item) => {
                return item.values['pk_entryapply'].value
            });

            if (compositepostdata && compositepostdata.content) {
                postData = Object.assign({}, postData, { content: compositepostdata })
            }


            try {
                let res = await dispatch({
                    type: 'emp/submitBill',
                    payload: {
                        postData: postData
                    }
                });
                if (res.success) {
                    if (res.data.content) {
                        dispatch({
                            type: 'emp/update',
                            payload: {
                                compositedata: res.data.content,
                                compositedisplay: true
                            }
                        })
                        return ;
                    } else {
                        dispatch({
                            type: 'emp/update',
                            payload: {
                                compositedata: null,
                                compositedisplay: false,
                                compositepostdata: null
                            }
                        })
                        if (this.checkErrorMsg(res)) {
                            this.toast({
                                color: 'success',
                                content: emp.language['gx6008-000047'] // 提交成功
                            });
                        }
                    }
                }
            }
            catch (e) {
            }
            this.pubSub.publish('getMainTableData');
        }
    }

    // 收回
    callbackOnes = async (selectedRows) => {
        const { props, action } = this.comp;
        const { dispatch, emp } = props;

        selectedRows = this.getSelectedRows(selectedRows);

        if (selectedRows) {
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk
            };

            postData.billids = selectedRows.map((item) => {
                return item.values['pk_entryapply'].value;
            });

            try {
                let res = await dispatch({
                    type: 'emp/callbackBill',
                    payload: {
                        postData: postData
                    }
                });

                if (res.success && this.checkErrorMsg(res)) {
                    this.toast({
                        color: 'success',
                        content: emp.language['gx6008-000048'] // 收回成功
                    });
                }
            }
            catch (e) {
            }
            this.pubSub.publish('getMainTableData');
        }
    }
    excuteOnes = (selectedRows) => {
        const { props, action } = this.comp;
        const { dispatch, emp } = props;
        selectedRows = this.getSelectedRows(selectedRows);
        if (selectedRows) {
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk
            };

            postData.billids = selectedRows.map((item) => {
                return item.values['pk_entryapply'].value;
            });
            this.promptBox({
                color: "warning",
                title: '确认执行',/* 国际化处理： 提示*/
                content: '确认要执行所选数据吗？',/* 国际化处理： 确认要执行所选数据吗？*/
                beSureBtnClick: async () => {
                    let res = await dispatch({
                        type: 'emp/excuteBill',
                        payload: {
                            postData: postData
                        }
                    });
    
                    if (res.success && this.checkErrorMsg(res)) {
                        this.toast({
                            color: 'success',
                            content: emp.language['gx6008-000112']  //'执行成功'
                        });
                    }
                    this.pubSub.publish('getMainTableData');
                }
            });
        } 
    }

    // 修改
    editOnes = (selectedRows) => {
        const { action } = this.comp;

        selectedRows = this.getSelectedRows(selectedRows);

        if (selectedRows) {
            action.formAct.toEditPage(selectedRows);
        }
    }

    // 删除主表格行
    deleteRows = async (selectRows) => {
        const { props, action } = this.comp;
        const { emp, dispatch } = props;
        let result = true;

        selectRows = action.rowAct.getSelectedRows(selectRows);
        if (selectRows) {
            let next = await new Promise((resolve, reject) => {
                this.promptBox({
                    color: "warning",
                    title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                    content: emp.language['gx6008-000019'],/* 国际化处理： 确定要删除所选数据吗？*/
                    beSureBtnClick: async () => {
                        resolve(true);
                    }
                });
            });
            if (!next) {
                return;
            }
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk
            };
            selectRows.map((item) => {
                postData.billids.push(item.values.pk_entryapply['value']);
            });

            try {
                let res = await dispatch({
                    type: 'emp/deleteTableRows',
                    payload: {
                        postData: postData
                    }
                });

                if (res.success && this.checkErrorMsg(res)) {
                    this.toast({
                        color: 'success',
                        content: emp.language['gx6008-000017'] // 删除成功
                    });
                }
            }
            catch (e) {
                result = false;
            }

            this.pubSub.publish('getMainTableData');

            return result;
        }
    }

    // 复制一个单据
    copyOnes = async (selectRows) => {
        const { props, action } = this.comp;
        const { emp, dispatch } = props;

        selectRows = action.rowAct.getSelectedRows(selectRows);
        if (selectRows) {
            let postData = {
                billid: selectRows[0].values['pk_entryapply'].value,
                pk_org: emp.orgValue.refpk
            };
            try {
                let res = await dispatch({
                    type: 'emp/copyOneBill',
                    payload: {
                        postData: postData
                    }
                });

                if (res.success && this.checkErrorMsg(res)) {
                    action.formAct.toEditPage(selectRows, res.data.form.head.card);
                }
            }
            catch (e) {
            }
        }
    }

    // 检查返回值里是否有 errorMsg
    checkErrorMsg = (res) => {
        if (res && res.data && (res.data.errorMsg || res.data.message)) {
            this.toast({
                color: 'danger',
                content: res.data.errorMsg || res.data.message
            });
            return false
        }

        return true;
    }

    // 查看审批详情
    previewApproveInfo = (selectedRows) => {
        const rowData = selectedRows[0].values;
        let billType = rowData['transtype'].value || rowData['pk_billtype'].value || '6101';
        let billId = selectedRows[0].values['pk_entryapply'].value;

        this.update({
            approveModalVisible: true,
            approveBillType: billType,
            approveBillId: billId
        });
    }
}